/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.mvc.views
{
	import com.puny.mvc.BaseObject;
	import com.puny.mvc.MvcEvent;
	
	import flash.events.Event;
	
	/**
	 * $Revision: 753 $<br/>
	 * Base view class that provides transition view logic.
	 * A class that exnteds <code>TransitionBaseView</code> has transition features.<br/>
	 * <br/>
	 * <code>MyTransitionView</code> is an example of a screen to wait for the given view loaded. After the given view are loaded,
	 * <code>MyTransitionView</code> will be removed automatically.
	 * @example
	 * <listing>
	public class MyTransitionView extends TransitionBaseView
	{
		private var _logo:MovieClip;
		public function MyTransitionView(view:Class)
		{
			super(view,"myassets.swf");
		}
		
		override protected function setup():void {
		    super.setup();
			_logo = getMovieClip("logo");
		}
		override public function afterReady():void {
			addChild(_logo);
			_logo.x = (stage.stageWidth - _logo.width) / 2;
			_logo.y = (stage.stageHeight - _logo.height) / 2;
		}
		
	}
	 * </listing>
	 */
	public class TransitionBaseView extends View
	{
		private var _class:Class;
		private var _view:BaseObject;
		private var _opened:Boolean = false;
		
		/**
		 * Constructor.
		 * The argument is a view class which is waited for by this class.
		 * @param cls view class name
		 * @param src name a swf file name, a MovieClipAsset instance, or a Class name
		 */
		public function TransitionBaseView(cls:Class,src:Object)
		{
			_class = cls;
			super(src);
		}
		
		override protected function setup():void {
			super.setup();
			if (_class != null) {
				_view = new _class();
			}
		}
		
		/**
		 * Override this when you need to do something before <code>READY</code> event.
		 */
		protected function beforeTransitionReady():void { }
		
		/**
		 * This method can't be overrode. Please override beforeTransitionReady() instead.
		 * @see #beforeTransitionReady
		 */
		final override public function beforeReady():void {
			beforeTransitionReady();
			if (_class != null && _view != null) {
				_view.addEventListener(MvcEvent.READY, openedHandler);
				_view.addEventListener(MvcEvent.LOADING, loadingHandler);
				trace(_view, "instance was created.");
				parent.addChildAt(_view,parent.numChildren-1);
			}
		}
		
		private function openedHandler(event:Event):void {
			_view.removeEventListener(MvcEvent.LOADING, loadingHandler);
			_opened = true;
		}
		
		/**
		 * Return <code>false</code> while main view is loaded, otherwise return <code>true</code>.
		 */
		protected function loadingHandler(event:Event):void {
			progress(int(_view.bytesLoaded*100/_view.bytesTotal));
			trace(_view.bytesLoaded,"/",_view.bytesTotal);
		}
		
		override public function procedureMainFrame():Boolean {
			return !_opened;
		}
		
		protected function progress(percent:int):void {
			
		}
		
		
		/**
		 * TransitionView will be closed right after procedureMainFrame returns <code>false</code>.
		 * Please override beforeClose() instead.
		 * This method can't be overrode.
		 * @see #procedureMainFrame
		 * @see com.puny.mvc.BaseObject#beforeClose
		 */
		final override public function afterMainFrame():void {
			complete();
			_view = null;
			_class = null;
		}
		
		/**
		 * Returns an instance of the main view that is given in this constructor
		 * @see #TransitionView
		 * @return instance of the view that will show up after this view closes.
		 */
		public function get mainView():BaseObject {
			return _view;
		}
	}
}